﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using SaturniLibrary.Web;

namespace Eleanor.Website.Utility.Filter
{
    public class UserLoginFilter : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            IController controller = filterContext.Controller;
            if ("Eleanor.Website.Areas.Admin.Controllers".Equals(controller.GetType().Namespace, StringComparison.OrdinalIgnoreCase))
            {
                if (!IsLoginAction(filterContext.ActionDescriptor.ActionName))
                {
                    if (SessionManager.Get("user") == null)
                    {
                        filterContext.Result = new RedirectResult("/admin/user/login");
                    }
                }
            }
        }

        private bool IsLoginAction(string actionName)
        {
            return "Login".Equals(actionName, StringComparison.OrdinalIgnoreCase)
                || "DoLogin".Equals(actionName, StringComparison.OrdinalIgnoreCase);
        }
    }
}
